package com.bsoft.template.web.assets;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bsoft.template.common.Result;
import com.bsoft.template.common.auth.UserInfo;
import com.bsoft.template.entity.assets.AssetsZj;
import com.bsoft.template.entity.auth.User;
import com.bsoft.template.entity.process.Approvalinfo;
import com.bsoft.template.entity.process.Process;
import com.bsoft.template.service.assets.AssetsZjService;
import com.bsoft.template.service.process.ApprovalinfoService;
import com.bsoft.template.service.process.ProcessService;
import com.bsoft.template.util.RequestParamPaser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("assetsZj")
public class AssetsZjController {

    @Autowired
    private AssetsZjService assetsZjService;

    @Autowired
    private ApprovalinfoService approvalinfoService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private UserInfo userInfo;

    @RolesAllowed({"dandang", "zhixing"})
    @PostMapping("/save")
    public Result save(@RequestBody AssetsZj assetsZj) {
        Result result;

        String role = userInfo.getRoleCode();
        Approvalinfo current = approvalinfoService.getApprovalinfo(assetsZj.getPzh());
        if (current != null && "dandang".equals(role)
                && !"dandang".equals(current.getSpjs()) && "1".equals(current.getSpzt())) {
            return new Result().fail();
        }
        if ("02".equals(assetsZj.getZt())) {
            return new Result().fail();
        }
        if (assetsZj.getPzh() == null) {

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String date = LocalDate.now().format(formatter);
            String maxId = assetsZjService.getMaxId(date.substring(0,4));
            String surfix = "0001";
            if (StringUtils.isNotBlank(maxId)) {
                surfix = String.format("%04d", Integer.parseInt(maxId.substring(8)) + 1);
            }
            assetsZj.setPzh(date + surfix);

            User user = userInfo.getUser();
            assetsZj.setZdr(user.getUsername());
            assetsZj.setZdrq(new Date());
            assetsZj.setZtrq(new Date());
            result = assetsZjService.save(assetsZj);

        }else{
            result = assetsZjService.updateById(assetsZj);
        }

        // 保存-插入流程
        if (current == null || !role.equals(current.getSpjs())) {
            Approvalinfo approvalinfo = new Approvalinfo();
            approvalinfo.setPzh(assetsZj.getPzh());
            approvalinfo.setSpzt("1");
            approvalinfoService.updateAudit(approvalinfo);
        }
        return result;

    }

    @GetMapping("/getList")
    public Result getList(HttpServletRequest request) {
        Map<String, String> params = RequestParamPaser.getParameters(request);

        return assetsZjService.getList(params);
    }

    @GetMapping("remove")
    public Result remove(String pzh) {
        return assetsZjService.removeById(pzh);
    }

    @GetMapping("/getAssetsZj")
    public Result getAssetsZj(String pzh) {
        QueryWrapper<AssetsZj> wrapper = new QueryWrapper<>();
        wrapper.eq("pzh", pzh);
        return assetsZjService.getOne(wrapper);
    }

    @GetMapping("/getAssetsHt")
    public Result getAssetsHt(String cfd,String fjh) {
        QueryWrapper<AssetsZj> wrapper = new QueryWrapper<>();
        wrapper.eq("cfd", cfd);
        wrapper.eq("fjh", fjh);
        wrapper.in("zt", "01","02");
        return assetsZjService.getMap(wrapper);
    }

    @GetMapping("updateZt")
    public Result updateZt(String pzh, String zt, String sm) {
        User user = userInfo.getUser();
        UpdateWrapper<AssetsZj> wrapper = new UpdateWrapper<>();
        wrapper.eq("pzh", pzh);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = LocalDate.now().format(formatter);
        String setSql = "zt='"+ zt+"',ztrq = '"+date+"'";
        if ("02".equals(zt)) {
            setSql = setSql+",JBR='"+user.getUsername()+"'";

            // 办理-插入流程
            String role = userInfo.getRoleCode();
            Approvalinfo current = approvalinfoService.getApprovalinfo(pzh);
            Process last = processService.getLastStep();
            if (role.equals(last.getRole()) && !role.equals(current.getSpjs())) {
                Approvalinfo approvalinfo = new Approvalinfo();
                approvalinfo.setPzh(pzh);
                approvalinfo.setSpzt("1");
                approvalinfoService.updateAudit(approvalinfo);
            }
        }
        if ("03".equals(zt) && StringUtils.isNotBlank(sm)) {
            setSql = setSql + ",BZ=concat(BZ, ' " + sm + "')";
        }
        wrapper.setSql(setSql);
        return assetsZjService.update(wrapper);
    }

    @GetMapping("getAuditCount")
    public Result getAuditCount() {
        return assetsZjService.getAuditCount();
    }

    @GetMapping("exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        assetsZjService.exportExcel(request, response);
    }

    @GetMapping("exportExcelAll")
    public void exportExcelAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        assetsZjService.exportExcelAll(request, response);
    }

    @GetMapping("getExpire")
    public Result getExpire() {
        return assetsZjService.getExpire();
    }

}
